package com.hzw.saas.common.storage.enums;

import java.util.Objects;

import cn.hutool.core.lang.Assert;

/**
 * 校验和获取模式
 *
 * @author EZ09
 * @since 07/29/2021
 */
public enum ChecksumModeEnum {

    WEAK_CHECKSUM(1, "弱校验和"),
    STRONG_CHECKSUM(2, "强校验和"),
    ALL_CHECKSUM(3, "强弱校验和"),

    ;
    private final Integer code;
    private final String text;

    ChecksumModeEnum(Integer code, String text) {
        this.code = code;
        this.text = text;
    }

    public Integer code() {
        return code;
    }

    public String text() {
        return text;
    }

    public static void assertValid(Integer code) {
        ChecksumModeEnum instance = instance(code);
        Assert.notNull(instance, "校验和模式异常");
    }

    public static ChecksumModeEnum instance(Integer code) {
        for (ChecksumModeEnum instance : ChecksumModeEnum.values()) {
            if (Objects.equals(instance.code(), code)) {
                return instance;
            }
        }
        return null;
    }

    public static ChecksumModeEnum instance(Integer code, ChecksumModeEnum defaultInstance) {
        ChecksumModeEnum instance = instance(code);
        return Objects.isNull(instance) ? defaultInstance : instance;
    }

    public static ChecksumModeEnum validInstance(Integer code) {
        ChecksumModeEnum instance = instance(code);
        return Objects.isNull(instance) ? ChecksumModeEnum.WEAK_CHECKSUM : instance;
    }
}
